package test5.lupeng.jf.com.teamdemo.activitys;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.bean.SignCalendar;
import test5.lupeng.jf.com.teamdemo.bean.request.MySignBean;
import test5.lupeng.jf.com.teamdemo.bean.request.MySignListBean;
import test5.lupeng.jf.com.teamdemo.bean.response.SignListResponse;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;
import test5.lupeng.jf.com.teamdemo.untils.RequestUtils;
import test5.lupeng.jf.com.teamdemo.untils.UserInfo;

/**
 * 签到的Activity
 * Created by jf on 2017/7/30.
 */

public class MySignActivity extends AppCompatActivity{

    private SignCalendar calendar;
    private String date;
    private int years;
    private String months;
    private Button btn_sign;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private String TAG = "MySignActivity";

    ArrayList<SignListResponse.ResultBean> mSignList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sing_my);
        ButterKnife.bind(this);
        //时间戳转化为标准格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        date = formatter.format(curDate);


        Log.e(TAG, "onCreate: " + date);
        calendar = (SignCalendar) findViewById(R.id.sc_main);
        btn_sign = (Button) findViewById(R.id.btn_sign);
        final TextView tv_date = (TextView) findViewById(R.id.tv_date);
        calendar.setUpdata(new SignCalendar.Update() {
            @Override
            public void update(int calendarYear, int calendarMonth) {
                tv_date.setText(calendarYear + "年" + calendarMonth + "月");
            }
        });
        getList();
        initView();
    }

    public void initView(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        getSupportActionBar().setTitle("签到");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void getList() {
        OkHttpClient client = new OkHttpClient();
        MySignListBean my = new MySignListBean(UserInfo.userId);
        String url = "https://leancloud.cn:443/1.1/functions/getMeSignInList";
        Request request = HTTPUtils.requestPost(my,url);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: " + result);
                SignListResponse sign = new Gson().fromJson(result, SignListResponse.class);
                mSignList.addAll(sign.getResult());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < mSignList.size(); i++) {

                            calendar.addMark(mSignList.get(i).getDateStr(), 0);
                            if (mSignList.get(i).getDateStr().equals(date)) {
                                btn_sign.setText("今日已签，明日继续");
                                //calendar.addMark(date,R.mipmap.ic_launcher);
                                btn_sign.setClickable(false);
                            }
                        }

                    }
                });
            }
        });
    }

    public void getData() {
        OkHttpClient client = new OkHttpClient();
        long timestamp = System.currentTimeMillis();
        MySignBean mySignBean = new MySignBean(UserInfo.userId, timestamp);
        String url = "https://leancloud.cn:443/1.1/functions/signIn";
        Request request = HTTPUtils.requestPost(mySignBean, url);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: " + result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        btn_sign.setText("今日已签，明日继续");
                        calendar.addMark(date, R.mipmap.ic_launcher);
                        btn_sign.setClickable(false);
                    }
                });

            }
        });

    }

    @OnClick(R.id.btn_sign)
    public void onViewClicked() {
        getData();
    }
}
